package liyuanchen.rabc.service.array;

import liyuanchen.rabc.domain.dto.RoleDto;
import liyuanchen.rabc.domain.entity.Role;
import liyuanchen.rabc.domain.vo.Result;
import liyuanchen.rabc.domain.vo.RoleVo;
import liyuanchen.rabc.service.IRoleService;
import liyuanchen.rabc.service.IUserService;


public class RoleService extends BaseService<Role, RoleDto, RoleVo>
        implements IRoleService {

    private RoleService() {
        data = new Role[DATA_SIZE + 1];
        temp = new RoleVo[DATA_SIZE];
    }



    private IUserService userService;

    private static IRoleService singleton;




    public static IRoleService getInstance() {
        if (singleton == null) {
            singleton = new RoleService();
        }
        return singleton;
    }
    @Override
    public RoleVo convertToVo(Role role) {
        return new RoleVo(role.getId(), role.name, role.dataScope);
    }
    @Override
    Role createEntity(RoleDto dto, int id) {
        return new Role(id, dto.name, dto.dataScope);
    }
    @Override
    void updateEntity(RoleDto dto, Role role) {
        role.name = dto.name;
        role.dataScope = dto.dataScope;
    }


    @Override
    public Result delete(int id) {
        if (!existsById(id)) {
            return Result.failure("删除失败，不存在该数据");
        }
        if (userService == null) {
            userService = UserService.getInstance();
        }
        if (userService.roleInUse(id)) {
            return Result.failure("删除失败，有⽤户具有该⻆⾊，不能删除");
        }
        data[id] = null;
        return Result.success("删除成功", null);
    }
}

